package com.price.design

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.PorterDuff
import android.graphics.PorterDuffXfermode
import android.graphics.Rect
import android.os.Build
import android.widget.ImageView
import androidx.core.content.ContextCompat
import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory


fun ImageView.roundImage(drawableId: Int, radius: Float,borderColor: Int?=null,border: Float?=null) {
    val src = decodeBitmapFromResource(context, drawableId) //获取Bitmap图片
    if (src != null) {
        val scaledBitmap=getScaleBitmap(src,(radius*2).toInt())
        val roundBitmap= scaledBitmap.getRoundBitmap((radius*2).toInt(),borderColor,border)
        setImageBitmap(roundBitmap)
    }
}

fun ImageView.roundImage(drawableId: Int, radius: Float) {
    val src = decodeBitmapFromResource(context, drawableId) //获取Bitmap图片
    if (src != null) {
        val scaledBitmap=getScaleBitmap(src,(radius*2).toInt())
        roundImage(scaledBitmap,radius)
    }
}

fun ImageView.roundImage(bitmap: Bitmap, radius: Float) {
    val roundedBitmapDrawable =
        RoundedBitmapDrawableFactory.create(resources, bitmap) //创建RoundedBitmapDrawable对象
    roundedBitmapDrawable.cornerRadius = radius //设置圆角Radius（根据实际需求）
    roundedBitmapDrawable.setAntiAlias(true) //设置抗锯齿

    setImageDrawable(roundedBitmapDrawable) //显示圆角
}

/**
 * 将drawableId转Bitmap
 * */
fun decodeBitmapFromResource(context: Context, drawableId: Int): Bitmap? {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
        return BitmapFactory.decodeResource(context.resources, drawableId)

    val drawable = ContextCompat.getDrawable(context, drawableId)
    val bitmap = Bitmap.createBitmap(
        drawable!!.intrinsicWidth,
        drawable.intrinsicHeight,
        Bitmap.Config.ARGB_8888
    )
    val canvas = Canvas(bitmap)
    drawable.setBounds(0, 0, canvas.width, canvas.height)
    drawable.draw(canvas)
    return bitmap
}

/**
 *
 * 获取裁剪后的圆形图片
 */
private fun getScaleBitmap(bmp: Bitmap, width: Int): Bitmap {
    var scaledSrcBmp: Bitmap?
    // 为了防止宽高不相等，造成圆形图片变形，因此截取长方形中处于中间位置最大的正方形图片
    val bmpWidth = bmp.width
    val bmpHeight = bmp.height
    var squareWidth = 0
    var squareHeight = 0
    var x = 0
    var y = 0
    var squareBitmap: Bitmap?
    if (bmpHeight > bmpWidth) { // 高大于宽
        squareHeight = bmpWidth
        squareWidth = squareHeight
        x = 0
        y = (bmpHeight - bmpWidth) / 2
        // 截取正方形图片
        squareBitmap = Bitmap.createBitmap(bmp, x, y, squareWidth, squareHeight)
    } else if (bmpHeight < bmpWidth) { // 宽大于高
        squareHeight = bmpHeight
        squareWidth = squareHeight
        x = (bmpWidth - bmpHeight) / 2
        y = 0
        squareBitmap = Bitmap.createBitmap(bmp, x, y, squareWidth, squareHeight)
    } else {
        squareBitmap = bmp
    }

    scaledSrcBmp = if (squareBitmap.width != width || squareBitmap.height != width) {
        Bitmap.createScaledBitmap(squareBitmap, width, width, true)
    } else {
        squareBitmap
    }

    squareBitmap = null
    return scaledSrcBmp
}

/**
 *
 * 获取裁剪后的圆形图片
 */
fun Bitmap.getRoundBitmap(width: Int,borderColor:Int?=Color.WHITE,border:Float?): Bitmap {

    var scaledSrcBmp:Bitmap = if(width!=this.width || width!=this.height){
        getScaleBitmap(this,width)
    }else{
        this
    }
    val output =
        Bitmap.createBitmap(scaledSrcBmp.width, scaledSrcBmp.height, Bitmap.Config.ARGB_8888)
    val canvas = Canvas(output)
    val paint = Paint()
    val rect = Rect(0, 0, scaledSrcBmp.width, scaledSrcBmp.height)
    paint.isAntiAlias = true
    paint.isFilterBitmap = true
    paint.isDither = true
    canvas.drawARGB(0, 0, 0, 0)
    canvas.drawCircle(
        (scaledSrcBmp.width / 2).toFloat(),
        (scaledSrcBmp.height / 2).toFloat(),
        (scaledSrcBmp.width / 2).toFloat(),
        paint
    )
    paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN)

    canvas.drawBitmap(scaledSrcBmp, rect, rect, paint)

    if(border!=null && border>0){
        paint.style=Paint.Style.STROKE
        paint.strokeWidth=border
        paint.color=borderColor!!
        val halfBorder=border/2
        canvas.drawArc(halfBorder,halfBorder,canvas.width-halfBorder,canvas.height-halfBorder,0f,360f,false,paint)
    }

    return output
}